<!DOCTYPE HTML>
<html>
<head>
    <title>报修单填写</title>
    <% include common/meta.ejs %>
    <% include common/public.ejs %>
</head>
<body ontouchstart="">
<div class="weui_cells_title">请填写您的报修信息</div>
<div class="weui_cells weui_cells_form">
    <form id="repairForm">
        <input type="hidden" name="status" value="1">
        <div class="weui_cell">
            <div class="weui_cell_hd"><label class="weui_label">报修类型</label></div>
            <div class="weui_cell_bd weui_cell_primary">
                <input class="weui_input" name="type" type="text" placeholder="报修类型" id="repairType">
            </div>
        </div>
        <!--<div class="weui_cell">
            <div class="weui_cell_hd"><label class="weui_label">联&nbsp;&nbsp;系&nbsp;&nbsp;人</label></div>
            <div class="weui_cell_bd weui_cell_primary">
                <input class="weui_input" type="text" name="issueUserName" placeholder="请输入姓名">
                <input class="weui_input" type="hidden" name="name" value="<%= userData.name%>">
            </div>
        </div>
        <div class="weui_cell">
            <div class="weui_cell_hd"><label class="weui_label">联系手机</label></div>
            <div class="weui_cell_bd weui_cell_primary">
                <input class="weui_input" type="text" name="issueUserPhone" placeholder="请输入手机号">
            </div>
        </div>
        <div class="weui_cell">
            <div class="weui_cell_hd"><label class="weui_label">房间标识</label></div>
            <div class="weui_cell_bd weui_cell_primary">
                <input class="weui_input" name="houseNumber_like" type="text" placeholder="输入房号" id="getHouse">
            </div>
        </div>
        <div class="weui_cell">
            <div class="weui_cell_hd"><label class="weui_label">报修地址</label></div>
            <div class="weui_cell_bd weui_cell_primary">
                <input class="weui_input" name="address" type="text" placeholder="报修地址" id="houseAddress">
            </div>
        </div>-->
        <div class="weui_cell">
            <div class="weui_cell_hd"><label class="weui_label">报修描述</label></div>
            <div class="weui_cell_bd weui_cell_primary">
                <input class="weui_input" name="remark" type="text" placeholder="报修描述">
            </div>
        </div>
        <!--选择图片-->
        <input type="hidden" name="id" value="<%= data.id %>"/>
        <div class="weui_cells_title">报修图片上传</div>
        <div class="weui_uploader" style="margin: 0.5rem;">
            <div class="weui_uploader_bd">
                <ul class="weui_uploader_files" id="weui_uploader_files">
                </ul>
                <div class="weui_uploader_input_wrp">
                    <input name="mediaIds" type="hidden" id="media_ids_input"/>
                    <!--<input name="icFrontImg" type="hidden" id="media_ids_input"/>-->
                    <!--<input name="icBackImg" type="hidden" id="media_ids_input"/>-->
                    <input class="weui_uploader_input" type="button" id="imageUpload"/>
                </div>
            </div>
        </div>
    </form>
</div>

<div style="text-align: center;width: 100%;position: fixed;bottom: 0">
    <a href="javascript:;" id="submitBtn" class="weui_btn weui_btn_warn">提交请求</a>
</div>
<% include common/footer.ejs %>
<script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script>
    "use strict";

    //提交报修
    $("#submitBtn").click(function () {
        //根据手机端登录人的信息，查找是否有相关房屋信息
        Dolphin.ajax({
            url: CTX + '/api/5ba55aa037574af58e688e8632a03a72@id=<%= userData.id%>',
            type: 'GET',
            forceUrl: 'true',
            onSuccess: function (resData) {

                console.log(resData);
                if(resData.value.properties.isSuccess){

                    let data = resData.value.properties;
                    let paramData = Dolphin.form.getValue("#repairForm");
                    paramData.issueUser = data.issueUser;
                    paramData.issueUserName = data.issueUserName;
                    paramData.issueUserPhone = data.issueUserPhone;
                    paramData.address = data.address;
                    paramData.handleUser = data.managerId;
                    paramData.handleUserName = data.managerName;
                    paramData.handleUserPhone = data.managerPhone;

                    let issueWechat = data.issueWechat;
                    let handleWechat = data.managerWechat;

                    console.log(paramData);
                    Dolphin.ajax({
                        url: CTX + '/api/fcca1aa0e7554b2bb871d3a1b8a7df36',
                        type: 'put',
                        forceUrl: 'true',
                        data: Dolphin.json2string(paramData),
                        onSuccess: function () {

                            //发送报修微信消息给管理员
                            var msgData = {
                                "url" : "http://"+Dolphin.wechatUrl+"/yams/view/butlerFix?openId="+handleWechat,
                                "openid" : handleWechat,
                                "first" : "管理员"+data.managerName+",您好!",
                                "keyword1" : paramData.type,
                                "keyword2" : data.managerName,
                                "keyword3" : data.managerPhone,
                                "remark" : "有新的报修清单，请及时处理！"
                            }

                            Dolphin.ajax({
                                url: '<%= global.config.contextPath == '/' ? '' : global.config.contextPath %>/wexin'
                                +'/sendWXTemplateMsg',
                                type: Dolphin.requestMethod.POST,
                                data: Dolphin.json2string(msgData),
                                forceUrl: true,
                                onSuccess: function (reData) {
                                    $.alert("添加成功");
                                    location.href=CTX+"/view/repairList";
                                }
                            })
                        }
                    })

                }else{
                    $.alert('对不起，没有查询到与您有关的房间！');
                }

            }
        });

    });

    //报修类型枚举选择
    $("#repairType").select({
        title: "选择报修类型",
        items: ["电器", "家具", "下水道"]
    });

    wx.config({
        debug: false,
        appId: "<%= data.appId %>", // 必填，公众号的唯一标识
        timestamp: "<%= data.timestamp %>",
        nonceStr: "<%= data.nonceStr %>",
        signature: "<%= data.signature %>",
        jsApiList: [  // 必填，需要使用的JS接口列表
            "chooseImage",
            "previewImage",
            "uploadImage",
            "downloadImage"
        ]
    });

    wx.ready(function () {
        $('#weui_uploader_files').click(function (e) {
            var image_files = $(this).children('.weui_uploader_file');
            if (image_files.length > 0) {
                var urls = [];
                image_files.each(function (i, li) {
                    urls.push($(li).attr('imgSrc'));
                })
                wx.previewImage({
                    current: $(e.target).attr('imgSrc'), // 当前显示图片的http链接
                    urls: urls // 需要预览的图片http链接列表
                });
            }
        });
        $('#imageUpload').click(function () {
            var total, panel = $('#weui_uploader_files');
            total = panel.children('.weui_uploader_file').length;
            wx.chooseImage({
                count: 5 - total, // 默认9
                sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
                sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
                success: function (res) {
                    var localIds = res.localIds,localId; // 返回选定照片的本地ID列表，localId可以作为img标签的src属性显示图片
                    if (localIds && localIds.length > 0) {
                        for(var i=0;i<localIds.length;i++){
                            localId = localIds[i];
                            wx.uploadImage({
                                localId: localId, // 需要上传的图片的本地ID，由chooseImage接口获得
                                isShowProgressTips: 1, // 默认为1，显示进度提示
                                success: function (res) {
                                    var serverId = res.serverId; // 返回图片的服务器端ID
                                    if (serverId) {
                                        Dolphin.ajax({
                                            url: '<%= global.config.contextPath == '/' ? '' : global.config.contextPath %>/wexin/downloadImage?catalog=repair&mediaId=' + serverId,
                                            type: Dolphin.requestMethod.GET,
                                            forceUrl: true,
                                            onSuccess: function (reData) {
                                                var value = $('#media_ids_input').val();
                                                if (value.length != 0) {
                                                    value += ',';
                                                }
                                                $('#media_ids_input').val(value + serverId);
                                                //http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=?&media_id=?
                                                $('<li class="weui_uploader_file" imgSrc="'+localId+'">' +
                                                    '<img src="'+reData.url+'" alt="'+serverId+'" />' +
                                                    '</li>')
                                                    .appendTo(panel);
                                                if (panel.children('.weui_uploader_file').length == 4) {
                                                    $('#imageUpload').hide();
                                                }
                                            }
                                        });
                                    }
                                }
                            });
                        }
                    }
                }
            });
        });
    });
    wx.error(function (res) {
        alert("<%= __('service.his.weixin.err') %>");
    });

</script>
</body>
</html>